/*
 * QTeXPreview
 * Copyright (C) 2008, Philip Pronin (philip.pronin@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "settings.h"

#include <QMutexLocker>

const QString Settings::fileName = "config/config.ini";
Settings* Settings::instance = NULL;
QMutex Settings::mutex;

Settings::Settings() {
	instance = this;

	settings = new QSettings(fileName, QSettings::IniFormat);
}

Settings::~Settings() {
	delete settings;
	instance = NULL;
}

Settings* Settings::getInstance() {
	QMutexLocker locker(&mutex);
	if (!instance)
		instance = new Settings();
	return instance;
}

void Settings::freeInstance() {
	if (!instance) return;
	delete instance;
	instance = NULL;
}

QByteArray Settings::getGeometry() const {
	QMutexLocker locker(&mutex);
	return settings->value("position/geometry").toByteArray();
}

void Settings::setGeometry(const QByteArray& data) {
	QMutexLocker locker(&mutex);
	settings->setValue("position/geometry", data);
}

bool Settings::getStayOnTop() const {
	QMutexLocker locker(&mutex);
	return settings->value("position/stayontop", false).toBool();
}

void Settings::setStayOnTop(bool enable) {
	QMutexLocker locker(&mutex);
	settings->setValue("position/stayontop", enable);
}

bool Settings::getSyntaxHighlighting() const {
	QMutexLocker locker(&mutex);
	return settings->value("editor/highlighting", true).toBool();
}

void Settings::setSyntaxHighlighting(bool enable) {
	QMutexLocker locker(&mutex);
	settings->setValue("editor/highlighting", enable);
}

bool Settings::getBracketsMatching() const {
	QMutexLocker locker(&mutex);
	return settings->value("editor/matching", true).toBool();
}

void Settings::setBracketsMatching(bool enable) {
	QMutexLocker locker(&mutex);
	settings->setValue("editor/matching", enable);
}

QByteArray Settings::getState() const {
	QMutexLocker locker(&mutex);
	return settings->value("position/state").toByteArray();
}

void Settings::setState(const QByteArray& data) {
	QMutexLocker locker(&mutex);
	settings->setValue("position/state", data);
}

QString Settings::getLocale() const {
	QMutexLocker locker(&mutex);
	return settings->value("app/locale", "").toString();
}

void Settings::setLocale(const QString& locale) {
	QMutexLocker locker(&mutex);
	settings->setValue("app/locale", locale);
}
